package com.zdd.ehe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zdd.ehe.EheApplication;
import com.zdd.ehe.param.TypeParam;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.AvgPriceService;
import com.zdd.ehe.service.KafkaService;
import com.zdd.ehe.vo.PricePredictToKafka;
import com.zdd.ehe.vo.PricePredictVo;
import com.zdd.ehe.vo.PriceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * @version 1.0.0
 * @title: PricePredictController
 * @author: HFLy
 * @description:
 * @date: 19/3/2023 下午3:29
 */
@Api(tags = "价格预测")
@RestController
@RequestMapping("/price'")
public class PricePredictController {
    @Autowired
    private AvgPriceService avgPriceService;
    @Autowired
    private KafkaService kafkaService;
    @PostMapping("/predict")
    @ApiOperation("价格预测")
    public Result predict(@RequestBody TypeParam typeParam){
        List<PriceVo> real = new ArrayList<>();
        List<PriceVo> predict = new ArrayList<>();
        List<Float> data = avgPriceService.near23(typeParam.getType());
        PricePredictToKafka pricePredictToKafka = new PricePredictToKafka(typeParam.getType(), data);
        String message = JSON.toJSONString(pricePredictToKafka);
        List<String> day23 = make23Day();
        for (int i = 0; i < 23; i++) {
            PriceVo priceVo = new PriceVo(day23.get(i), data.get(i));
            real.add(priceVo);
        }
        //反转 real 数组
        Collections.reverse(real);
        List<String> day7 = make7Day();
        EheApplication.FINISH_PRICE = false;
        //获取锁
        while (!EheApplication.PRICE_LOCK){
            EheApplication.PRICE_LOCK = true;
            kafkaService.sendToPrice(message);
            while (!EheApplication.FINISH_PRICE){
                continue;
            }
            EheApplication.PRICE_LOCK = false;
            break;
        }
        JSONObject jsonObject = JSON.parseObject(EheApplication.PRICE_RESULT);
        JSONArray array = (JSONArray) jsonObject.get("data");
        List<Float> result = array.toJavaList(Float.class);
        for (int i = 0; i < 7; i++) {
            PriceVo priceVo = new PriceVo(day7.get(i), result.get(i));
            predict.add(priceVo);
        }
        //计算增长率
        List<Float> growthRate = new ArrayList<>();
        growthRate.add((result.get(0)-data.get(0))/data.get(0));
        for (int i = 1; i < 7; i++) {
            Float rate = (result.get(i)-result.get(i-1))/result.get(i-1);
            growthRate.add(rate);
        }
        PricePredictVo pricePredictVo = new PricePredictVo(real, predict, growthRate);
        return Result.success("查询成功",pricePredictVo);

    }

    public List<String> make23Day(){
        List<String> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i=1;i<=23 ; i++){
            calendar.add(Calendar.DATE, -1);
            String date = simpleDateFormat.format(calendar.getTime());
            result.add(date);
        }
        return result;
    }

    public List<String> make7Day(){
        List<String> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        result.add(simpleDateFormat.format(calendar.getTime()));
        for (int i=1;i<=6 ; i++){
            calendar.add(Calendar.DATE, 1);
            String date = simpleDateFormat.format(calendar.getTime());
            result.add(date);
        }
       return result;
    }
}
